04. If...Else Statements
If…else statements
If…else statements allow you to execute certain pieces of code based on a condition, or set of conditions, being met.
if (/* this expression is true */) {
// run this code
} else {
// run this code
}
This is extremely helpful because it allows you to choose which piece of code you want to run based on the result of an expression. For example,
var a = 1;
var b = 2;
if (a > b) {
console.log("a is greater than b");
} else {
console.log("a is less than or equal to b");
}
Prints: "a is less than or equal to b"
A couple of important things to notice about if...else
statements.
The value inside the if
statement is always converted to true or false. Depending on the value, the code inside the if
statement is run or the code inside the else
statement is run, but not both. The code inside the if
and else
statements are surrounded by curly braces {...}
to separate the conditions and indicate which code should be run.
TIP: When coding, sometimes you may only want to use an
if
statement. However, if you try to use only anelse
statement, then you will receive the errorSyntaxError: Unexpected token else
. You’ll see this error becauseelse
statements need anif
statement in order to work. You can’t have anelse
statement without first having anif
statement.